package org.cybzacg.convert.scanner;


import org.cybzacg.convert.annotation.Converter;
import org.cybzacg.convert.enums.ConversionPriority;

import java.util.Arrays;
import java.util.Set;

/**
 * 预定义的过滤器
 */
public class DefaultFilters {

    /**
     * 启用的转换器过滤器
     */
    public static final Filter ENABLED_ONLY = clazz -> {
        Converter annotation = clazz.getAnnotation(Converter.class);
        return annotation != null && annotation.enabled();
    };

    /**
     * 按优先级过滤
     *
     * @param minPriority 最小优先级
     * @return 过滤器
     */
    public static Filter priorityAtMost(ConversionPriority minPriority) {
        return clazz -> {
            Converter annotation = clazz.getAnnotation(Converter.class);
            return annotation != null &&
                    annotation.priority().getValue() <= minPriority.getValue();
        };
    }

    /**
     * 按标签过滤
     *
     * @param requiredTags 必需的标签
     * @return 过滤器
     */
    public static Filter hasTags(String... requiredTags) {
        return clazz -> {
            Converter annotation = clazz.getAnnotation(Converter.class);
            if (annotation == null || requiredTags.length == 0) {
                return true;
            }

            Set<String> converterTags = Set.of(annotation.tags());
            return Arrays.stream(requiredTags)
                    .allMatch(converterTags::contains);
        };
    }

    /**
     * 按作者过滤
     *
     * @param author 作者名称
     * @return 过滤器
     */
    public static Filter byAuthor(String author) {
        return clazz -> {
            Converter annotation = clazz.getAnnotation(Converter.class);
            return annotation != null &&
                    (author.isEmpty() || author.equals(annotation.author()));
        };
    }
}
